package com.pinyoumei.app.adapter;

import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.pinyoumei.app.R;
import com.pinyoumei.app.bean.SourceMaterialBean;
import com.pinyoumei.app.network.Constants;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Walter on 2020-08-11.
 */
public class SourceMaterialRecycleAdapter extends BaseQuickAdapter<SourceMaterialBean.ResultBean.InfoListBean, BaseViewHolder> {
    private List<SourceMaterialBean.ResultBean.InfoListBean> data;

    public SourceMaterialRecycleAdapter(@Nullable List<SourceMaterialBean.ResultBean.InfoListBean> data) {
        super(R.layout.item_sourcematerial_recycle, data);
        this.data = data;
    }

    @Override
    protected void convert(BaseViewHolder helper, SourceMaterialBean.ResultBean.InfoListBean item) {
        helper.setText(R.id.item_source_content,item.getContent());
        helper.setText(R.id.item_source_name,item.getPublisher_name());
        helper.setText(R.id.item_source_time,item.getPublish_time());
        ImageView avatar = helper.getView(R.id.item_source_avatar);
        Glide.with(mContext).load( item.getHead_img()).into(avatar);

        RelativeLayout singleLayout = helper.getView(R.id.item_source_single_layout);
        ImageView singleIv = helper.getView(R.id.item_source_single_image);  //视频第一帧或者单张图片
        ImageView videoPlayIv = helper.getView(R.id.item_source_video_play);  //视频上的播放按钮
        RecyclerView recyclerView = helper.getView(R.id.item_item_source_recycle);  //多张图片的布局

        helper.getView(R.id.item_source_content).setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (listener!=null)
                    listener.textLongClick(item.getContent());
                return false;
            }
        });
        helper.getView(R.id.item_source_share).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null)
                    listener.moreClick(data.indexOf(item));
            }
        });

        String[] imgs = item.getImg_path_str().split(",");
//        Log.e("LogUtils", "imgs  " + imgs.length);
        if (item.getContent_type() == 2) {
            singleLayout.setVisibility(View.VISIBLE);
            videoPlayIv.setVisibility(View.VISIBLE);
            recyclerView.setVisibility(View.GONE);
            Glide.with(mContext).setDefaultRequestOptions(new RequestOptions().frame(4000000).centerCrop())
                    .load(Constants.MAIN_URL + item.getImg_path_str())
                    .into(singleIv);
            singleLayout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null)
                        listener.videoClick(item.getImg_path_str());
                }
            });
        } else {
            if (imgs.length <= 1) {
//                if (item.getImages().size() == 0)
//                    return;
                Glide.with(mContext).load(Constants.MAIN_URL + imgs[0]).into(singleIv);
                singleLayout.setVisibility(View.VISIBLE);
                recyclerView.setVisibility(View.GONE);
                String[] finalImgs = imgs;
                singleLayout.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (listener != null)
                            listener.imageClick(finalImgs[0]);
                    }
                });
            } else {
                singleLayout.setVisibility(View.GONE);
                recyclerView.setVisibility(View.VISIBLE);
                if (imgs.length == 4) {
                    String[] temp = imgs;
                    imgs = new String[temp.length + 1];
                    imgs[0] = temp[0];
                    imgs[1] = temp[1];
                    imgs[2] = "";
                    imgs[3] = temp[2];
                    imgs[4] = temp[3];
                }
                ArrayList<String> imgsList = new ArrayList<>();
                for (int i = 0; i < imgs.length; i++) {
                    imgsList.add(imgs[i]);
                }
                SourceMaterialImageRecycleAdapter adapter = new SourceMaterialImageRecycleAdapter(imgsList);
                recyclerView.setLayoutManager(new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL));
                recyclerView.setAdapter(adapter);
                adapter.setListener(new SourceMaterialImageRecycleAdapter.SourceMaterialListener() {
                    @Override
                    public void imageClick(String url) {
                        if (listener != null)
                            listener.imageClick(url);
                    }
                });
            }
        }

    }

    public void setListener(SourceMaterialListener listener) {
        this.listener = listener;
    }

    public SourceMaterialListener listener;

    public interface SourceMaterialListener {

        void textLongClick(String content);

        void imageClick(String url);

        void videoClick(String url);

        void moreClick(int positin);
    }
}
